package com.zmj.mobile.process;

import com.zmj.framework.response.Result;
import com.zmj.framework.util.ThreadContextHandler;
import com.zmj.mobile.dto.AuditDTO;
import com.zmj.mobile.entity.OrgEntity;
import com.zmj.mobile.entity.SealApplyEntity;
import com.zmj.mobile.mapper.ISealApplyMapper;
import com.zmj.mobile.service.IOrgService;
import com.zmj.modules.flow.entity.dto.BpmProcessStartQueryDto;
import com.zmj.modules.flow.entity.dto.BpmTaskQueryDto;
import com.zmj.modules.flow.entity.vo.ProcessInstanceVO;
import com.zmj.modules.flow.service.FlowableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class SealAuditServiceImpl implements IAuditService {

    @Resource
    private FlowableService flowableService;
    @Resource
    private ISealApplyMapper sealApplyMapper;
    @Resource
    private IOrgService iOrgService;

    public static final String JIA_XIAN_LONG = "贾先龙";
    public static final String YUAN_LI = "袁丽";
    public static final String SHEN_DONG_QU_YU = "神东区域";

    public static final String PROCESS_01 = "YZSQONE";
    public static final String PROCESS_02 = "YZSQTWO";
    public static final String PROCESS_03 = "YZSQTHREE";

    @Override
    public boolean agree(IAudit audit) {
        AuditDTO auditDTO = (AuditDTO) audit;
        Long id = auditDTO.getId();

        SealApplyEntity sealApplyEntity = sealApplyMapper.selectById(((AuditDTO) audit).getId());

        if (Objects.isNull(sealApplyEntity)) {
            return false;
        }

        //调用流程接口进入审核流程
        BpmProcessStartQueryDto bpmProcessStartQueryDto = getBpmProcessStartQueryDto(sealApplyEntity);
        Result<ProcessInstanceVO> result = flowableService.startProcessInstance(bpmProcessStartQueryDto);
        // 流程的id
        String procInstId = result.getData().getProcInstId();
        //todo
        SealApplyEntity entity = SealApplyEntity.builder().id(id).dataStatus("").flowId(procInstId).build();
        sealApplyMapper.updateById(entity);
        // 任务处理ID
        String username = ThreadContextHandler.getCurrentUser().getUsername();
        String taskId = result.getData().getUserTaskInfos().get(0).getTaskId();
        BpmTaskQueryDto bpmTaskQueryDto = new BpmTaskQueryDto(taskId, username, null, "", null, null);
        flowableService.agree(bpmTaskQueryDto);
        return true;

    }

    @Override
    public boolean reject(IAudit audit) {
        return false;
    }

    private BpmProcessStartQueryDto getBpmProcessStartQueryDto(SealApplyEntity sealApplyEntity) {
        String username = ThreadContextHandler.getCurrentUser().getUsername();
        Optional<OrgEntity> orgEntityOptional = iOrgService.getByUserName(username);

        Map<String, Object> map = new HashMap<>();
        map.put("businessKey", sealApplyEntity.getId());
        if (orgEntityOptional.isPresent()) {
            map.put("starterOrg", orgEntityOptional.get().getOrgCode());
        }
        map.put("yyType", sealApplyEntity.getSealType());
        map.put("money", sealApplyEntity.getSealAmount());
        //默认是这个审批流
        String processKey = PROCESS_03;

        if (YUAN_LI.equals(sealApplyEntity.getCreateUserName()) || JIA_XIAN_LONG.equals(sealApplyEntity.getCreateUserName())) {
            //当发起人是贾先龙或袁丽
            processKey = PROCESS_01;
        } else if (SHEN_DONG_QU_YU.equals(sealApplyEntity.getDepartName())) {
            //当发起人所属部门是神东区域
            processKey = PROCESS_02;
        }

        BpmProcessStartQueryDto bpmProcessStartQueryDto = new BpmProcessStartQueryDto();
        bpmProcessStartQueryDto.setVariables(map);
        bpmProcessStartQueryDto.setCurrentUserId(username);
        bpmProcessStartQueryDto.setKey(processKey);
        return bpmProcessStartQueryDto;
    }
}
